package com.etime.shyauth.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etime.shyauth.dao.OrganizationMapper;
import com.etime.shyauth.dao.SalerMapper;
import com.etime.shyauth.dao.SysFunctionMapper;
import com.etime.shyauth.dao.TeacherMapper;
import com.etime.shyauth.pojo.Organization;
import com.etime.shyauth.pojo.Saler;
import com.etime.shyauth.pojo.SysFunction;
import com.etime.shyauth.pojo.Teacher;
import com.etime.shyauth.services.interfaces.CurrentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理登录用户的service层实现类
 */
@Service
public class CurrentUserServiceImpl implements CurrentUserService {
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private SalerMapper salerMapper;
    @Autowired
    private SysFunctionMapper sysFunctionMapper;
    /**
     * 根据用户id查询机构
     *
     * @param userid 用户id
     * @return 返回null表示不是机构管理员
     */
    @Override
    public Organization selectorg(Integer userid) {
        QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        wrapper.eq("orguserid",userid);
        List<Organization> list = organizationMapper.selectList(wrapper);
        if(list!= null && list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 根据用户id查询教师
     *
     * @param userid 用户id
     * @return 返回null表示不是教师
     */
    @Override
    public Teacher selectTec(Integer userid) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        List<Teacher> list = teacherMapper.selectList(wrapper);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 根据用户id查询销售人员信息
     *
     * @param userid 用户id
     * @return 返回null表示不是销售人员
     */
    @Override
    public Saler selectSaler(Integer userid) {
        QueryWrapper<Saler> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        List<Saler> list = salerMapper.selectList(wrapper);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }

    /**
     * 根据用户id查询用户所具有的权限（用于初始化功能列表）
     *
     * @param userid 用户id
     * @return
     */
    @Override
    public List<SysFunction> selectLoginUserRight(Integer userid) {
        if(userid==-1){
            return sysFunctionMapper.selectAdminFunction();
        }else {
            return sysFunctionMapper.selectUserFunction(userid);
        }
    }
}
